package br.uff.es2.xadrez.ia;

import br.uff.es2.xadrez.core.Board;
import br.uff.es2.xadrez.core.King;
import br.uff.es2.xadrez.core.Pawn;
import br.uff.es2.xadrez.core.Piece;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author Thales
 */
public class OutraIa {

    public int[][] move(Board board)
    {
        int[] oldPosition = new int[2];
        int[] chosenPosition = new int[2];
        int[] newPosition = new int[2];
        int bestValue = -5000;

        Collection<int[]> validPiecesPositions = new ArrayList<int[]>();
        Collection<int[]> validMovesPositions = new ArrayList<int[]>();
        Collection<int[]> validOldPositions = new ArrayList<int[]>();

        for (int i = 0; i < board.getBoard().length; i++) {
            Piece[] pieces = board.getBoard()[i];
            for (int j = 0; j < pieces.length; j++) {
               Piece piece = pieces[j];
               if(piece != null && piece.getColor().equals(board.getTurn()))
               {
                   int[] validPosition = {i,j};
                   validPiecesPositions.add(validPosition);
               }
            }
        }
        if(validPiecesPositions.size() == 0)
            return null;

        for (int index = 0; index < validPiecesPositions.size(); index++) {
            oldPosition = (int[]) validPiecesPositions.toArray()[index];
            Piece piece = board.getBoard()[oldPosition[0]][oldPosition[1]];
            int[][] avaiableMoves = piece.getAvaiableMoves(oldPosition, board.getBoard());
            for (int i = 0; i < avaiableMoves.length; i++) {
                for (int j = 0; j < avaiableMoves.length; j++) {
                    if(avaiableMoves[i][j] == 1)
                    {
                        int[] validMove = {i,j};
                        Piece otherPiece = board.getBoard()[i][j];
                        board.setPiece(piece, validMove);
                        board.setPiece(null, oldPosition);
                        if (verifyCheck(board.getBoard()) == 0) {
                            int value = evaluationFunction(board);
                            if (value > bestValue) {
                                bestValue = value;
                                validMovesPositions.clear();
                                validOldPositions.clear();
                                validMovesPositions.add(validMove);
                                validOldPositions.add(oldPosition);
                            } else {
                                if (value == bestValue) {
                                    validMovesPositions.add(validMove);
                                    validOldPositions.add(oldPosition);
                                }
                            }
                        }
                        board.setPiece(otherPiece, validMove);
                        board.setPiece(piece, oldPosition);
                    } //else {
//                        if (avaiableMoves[i][j] == 4) {
//                            int[] validMove = {i,j};
//                            validMovesPositions.add(validMove);
//                            validOldPositions.add(oldPosition);
//                        }
//                    }
                }
            }
        }

        if(validMovesPositions.size() > 0) {
            double random = Math.random();
            int index = (int) Math.floor(random*validMovesPositions.size());
            newPosition = (int[]) validMovesPositions.toArray()[index];
            chosenPosition = (int[]) validOldPositions.toArray()[index];
        } 
        int[][] pieceMove = {chosenPosition, newPosition};
        return(pieceMove);
    }

    private int evaluationFunction(Board board) {//, int[] newPosition) {
        int soma = 0;
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (board.getBoard()[i][j] != null) {
                    if (board.getBoard()[i][j].getColor().equals(Piece.Color.BLACK)) {
                        soma += board.getBoard()[i][j].getValue();
                    } else {
                        soma -= board.getBoard()[i][j].getValue();
                    }
                }
            }
        }
        return soma;
    }

    int verifyCheck(Piece[][] board) {
        int[][] blackCheckBoard = new int[8][8];
        int[][] matriz = new int[8][8];
        int[] position = new int[2];
        int[] kingPosition = new int[2];
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (board[i][j] != null) {
                    position[0] = i;
                    position[1] = j;
                    if (board[i][j] instanceof Pawn) {
                        matriz = ((Pawn) board[i][j]).getCaptureMoves(position, board);
                    } else {
                        matriz = board[i][j].getAvaiableMoves(position, board);
                    }
                    if (board[i][j].getColor().equals(Piece.Color.BLACK)) {
                        if (board[i][j] instanceof King) {
                            kingPosition[0] = i;
                            kingPosition[1] = j;
                        }
                    } else {
                        somaMatriz(blackCheckBoard, matriz);
                    }
                }
            }
        }
        if (blackCheckBoard[kingPosition[0]][kingPosition[1]] > 0) {
            return 1;
        }
        return 0;
    }

    void somaMatriz(int[][] checkBoard, int[][] matriz) {
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                checkBoard[i][j] += matriz[i][j];
            }
        }
    }
}
